package com.yanhui.api.voice;

import com.alibaba.fastjson.JSON;
import com.yanhui.modal.video.KuwoMusicDatas;
import com.yanhui.modal.video.YinyuetaiOst;
import com.yanhui.utils.http.HttpRequest;
import com.yanhui.utils.http._cmsUtils;

import java.util.List;

/**
 * @Description 音频技术
 * @Author yanhui
 * @Date 2018-11-15 14:13
 */
public class VoiceApi {
    private static final String YINYUETAI_URL = "http://47.98.237.162/share/api/video/v1/getYinyuetaiOstList";
    private static final String KUWO_LIST_URL = "http://47.98.237.162/share/api/video/v1/getKuwoMusicList";


    public static void main(String[] args) {
        getLoveqMp3(4,"D:/");
//        getYinyuetaiOstList();
//        KuwoMusicDatas k = getKuwoMusicList();
//        System.out.println(k.toString());
//        http://sou.kuwo.cn/ws/NSearch?type=all&catalog=yueku2016&key=%E9%A3%9E%E8%99%8E


//        String path = "D://1";
//        String mp3filepath = path + ".mp3";
//        String pcmfilepath = path + ".pcm";
//
//        try {
//            convertMP32PCM(mp3filepath, pcmfilepath);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 获取酷我音乐列表前10个,后续可能会加SECRET_KEY
     * @return
     */
    public static KuwoMusicDatas getKuwoMusicList(){
        return JSON.parseObject(HttpRequest.
                searchByUrl(KUWO_LIST_URL), KuwoMusicDatas.class);
    }

    /**
     * 获取音悦台影视的mv列表前10个,后续可能会加SECRET_KEY
     * @return
     */
    public static YinyuetaiOst getYinyuetaiOstList(){
        return JSON.parseObject(HttpRequest.
                searchByUrl(YINYUETAI_URL),YinyuetaiOst.class);
    }

    /**
     * 获取loveQ的mp3音频 3.0
     * 无需登录即可下载mp3文件 ps要支持原创
     * @param size 获取条数 建议越少越好，不然链接超时
     * @param filePath 文件保存路径
     */
    public static void getLoveqMp3(int size, String filePath){
        _cmsUtils cms = new _cmsUtils();
        // 1、获取官网4个mp3列表
        String url = "http://www.loveq.cn/program.html";
        String html = cms.getHtmlResourceByUrl(url, "utf-8");
        String pat = "<dl class=\"clearfix\" id=\"(.+?)\">\n" +
                "                    \t<dt style=\"width:10%; padding-right:20px;\"><a href=\"(.+?)\" target=\"_blank\" title=\"(.+?)\">(.+?)</a></dt>\n" +
                "                        <dd style=\"width:58%; padding-left:20px;\" class=\"ct\"><span>(.+?)</span>\n" +
                "                        </dd>\n" +
                "                        \n" +
                "                        <dd style=\"width:7%;\">(.+?)</dd>\n" +
                "                        <dd style=\"width:5%; padding-left:5px;\"><a href=\"(.+?)\" title=\"(.+?)\"><img src=\"/themes/loveq/loveautumn/appimg/download.jpg\" border=\"0\"></a></dd>\n" +
                "                    </dl>";
        String title = "programId, downLoadUrl, title, date, title2, num, url2, title3";
        List<String> list = cms.getCode(html, title, pat);
        list = list.subList(0, size);
        // 2、拼接下载地址，下载mp3文件到本地
//        String programUrl = "http://dl.loveq.cn/program/2018/LoveQ.cn_DATE-1.mp3"; //2018-12-17 失效
//        String programUrl = "http://dl.loveq.cn/live/program/2018/LoveQ.cn_DATE-1.mp3";
        String programUrl = "http://dl.loveq.cn/program/2018/LoveQ.cn-DATE-1.mp3"; //2019-1-1
        for(String str : list){
            String[] str_ = str.split(",");
            String mp3title = str_[4].replace("【MP3格式】","");
            String date = str_[3].replaceAll("\\.","-");
            String downLoadUrl = programUrl.replace("DATE", date);
            /**
             * java.net.ConnectException: Connection timed out: connect
             * 下载文件大，可能会出现超时
             * 解决方法，每次下载一个，一小时内60分钟下载4次，每15分钟下载一个mp3文件
             */
            System.out.println("正在下载：" + downLoadUrl);
            cms.downLoadUrl(filePath, downLoadUrl);
            System.out.println(mp3title + "已下载好");
        }
    }




}
